﻿/*
 * Created by SharpDevelop.
 * User: Matjaz
 * Date: 10.11.2007
 * Time: 18:57
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace gopher
{
	/// <summary>
	/// Description of TekstovnoOkno.
	/// </summary>
	public partial class TekstovnoOkno : Form
	{
		public string mojStreznik;
		public string mojSelektor;
		public string mojaVrata;
		public int mojTimeout;

        printersupport _printdocument = new printersupport();
		
		public TekstovnoOkno(string selektor, string streznik, string vrata, int iTimeout)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			mojStreznik = streznik;
			mojSelektor = selektor;
			mojaVrata = vrata;
			mojTimeout = iTimeout;
		}
		
		static string ReadLine (Stream s)
		{
			List<byte> lineBuffer = new List<byte>();
			while (true)
			{
				int b = s.ReadByte();
				if (b == 10 || b < 0) break;
				if (b != 13) lineBuffer.Add ((byte)b);
			}
			//return Encoding.UTF8.GetString (lineBuffer.ToArray());
			return Encoding.ASCII.GetString (lineBuffer.ToArray());
		}

		static void SendCommand (Stream stream, string line)
		{
			//byte[] data = Encoding.UTF8.GetBytes (line + "\r\n");
			byte[] data = Encoding.ASCII.GetBytes (line + "\r\n");
			stream.Write (data, 0, data.Length);
		}
		
		void TekstovnoOknoLoad(object sender, EventArgs e)
		{
			using (TcpClient client = new TcpClient (mojStreznik, Convert.ToInt32(mojaVrata)))
			using (NetworkStream n = client.GetStream())
			{
				SendCommand (n, mojSelektor);				
				List<string> imenik = new List<string>();
				StringBuilder SB = new StringBuilder();
				int x=1000;
				if (n.DataAvailable == false) Thread.Sleep(mojTimeout);
				textBox1.Cursor = Cursors.WaitCursor;
				while (n.DataAvailable)
				{
					string line = ReadLine (n);
					if (line == ".") break;		
					SB.Append(line);
					SB.Append("\r\n");
					x--;
					if (n.DataAvailable == false) Thread.Sleep(mojTimeout);
				}
				textBox1.Text = SB.ToString();
				textBox1.Select(0,0);
				textBox1.Cursor = Cursors.Default;
			}
			this.Text = mojSelektor.Substring(mojSelektor.LastIndexOf("\\")+1);
		}

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintDialog pd = new PrintDialog();
            _printdocument.Text = textBox1.Text;
            _printdocument.Font = textBox1.Font;
            pd.Document = _printdocument;
            if (pd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    _printdocument.Print();
                }
                catch (Exception printException)
                {
                    System.Windows.Forms.MessageBox.Show("Printing error\r\n\r\n" + printException.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                }
            }
        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintPreviewDialog ppd = new PrintPreviewDialog();
            _printdocument.Text = textBox1.Text;
            _printdocument.Font = textBox1.Font;
            ppd.Document = _printdocument;
            ppd.ShowDialog();

        }

        private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PageSetupDialog psd = new PageSetupDialog();
            psd.Document = _printdocument;
            psd.ShowDialog();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = ("Text file (*.txt)|*.txt|All files (*.*)|*.*");
            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(saveDialog.FileName);
                sw.Write(textBox1.Text);
                sw.Close();
            }
        }
		
		void CopyToolStripButtonClick(object sender, EventArgs e)
		{
			textBox1.Copy();
		}
		
		void ExitToolStripMenuItemClick(object sender, EventArgs e)
		{
			Close();
		}
		
		void SelectAllToolStripMenuItemClick(object sender, EventArgs e)
		{
			textBox1.SelectAll();
		}
		
		void CopyToolStripMenuItemClick(object sender, EventArgs e)
		{
			textBox1.Copy();
		}
		
		void TekstovnoOknoHelpRequested(object sender, HelpEventArgs hlpevent)
		{
			Help.ShowHelp(this, Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"))+"\\Help\\gopher.chm", "html\\text-window.htm");
		}
	}
}
